<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="工程名称" prop="projectId">
        <el-select
          v-model="queryParams.projectId"
          :loading="loading" placeholder="请选择工程名称" clearable>
          <el-option
            v-for="item in projectOptions"
            :key="item.value"
            :label="item.projectName"
            :value="item.projectId">
          </el-option>
        </el-select>
      </el-form-item>-->
      <el-form-item label="工程名称" prop="projectName">
        <el-input
          v-model="queryParams.projectName"
          placeholder="请输入工程名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="获奖名称" prop="awardName">
        <el-input
          v-model="queryParams.awardName"
          placeholder="请输入获奖名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="获奖等级" prop="awardGrade">
        <el-select v-model="queryParams.awardGrade" placeholder="请选择获奖等级" clearable>
          <el-option
            v-for="dict in dict.type.pm_award_grade"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="颁奖部门" prop="awardDepartment">
        <el-input
          v-model="queryParams.awardDepartment"
          placeholder="请输入颁奖部门"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="获奖日期">
        <el-date-picker
          v-model="daterangeAwardDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="负责人" prop="leader">
        <el-input
          v-model="queryParams.leader"
          placeholder="请输入负责人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="到期日期" v-show="showHighSearch">
        <el-date-picker
          v-model="daterangeExpireDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="success" icon="el-icon-search" size="mini" @click='showHighSearch=!showHighSearch'>高级搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:current_project_award:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:current_project_award:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:current_project_award:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:current_project_award:export']"
        >导出</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="current_project_awardList" @selection-change="handleSelectionChange" @sort-change="sortChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="工程名称" align="left" header-align="center" prop="projectName" width="350" fixed/>
      <el-table-column label="获奖名称" align="center" prop="awardName" />
      <el-table-column label="获奖等级" align="center" prop="awardGrade">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pm_award_grade" :value="scope.row.awardGrade"/>
        </template>
      </el-table-column>
      <el-table-column label="颁奖部门" align="center" prop="awardDepartment" />
      <el-table-column label="获奖日期" align="center" prop="awardDate" width="180" sortable="true">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.awardDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="到期日期" align="center" prop="expireDate" width="180" sortable="true">
        <template slot-scope="scope">
          <span v-if="scope.row.expire ==1">{{ parseTime(scope.row.expireDate, '{y}-{m}-{d}') }}</span>
          <span style="color: red;font-weight: bolder;" v-if="scope.row.expire ==0">{{ parseTime(scope.row.expireDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="负责人" align="center" prop="leader" />
      <el-table-column label="证书" align="center" prop="image" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.image" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:current_project_award:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:current_project_award:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改项目获奖对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
        <el-form-item label="工程名称" prop="projectId">
          <el-select
            v-model="form.projectId"
            filterable
            remote
            reserve-keyword
            placeholder="请输入工程名称"
            :remote-method="getProjectList"
            :loading="loading">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.projectName"
              :value="item.projectId">
            </el-option>
          </el-select>
        </el-form-item>
          </el-col>
          <el-col :span="12">

        <el-form-item label="获奖名称" prop="awardName" style="width:320px;">
          <el-input v-model="form.awardName" placeholder="请输入获奖名称" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
        <el-form-item label="获奖等级" prop="awardGrade">
          <el-radio-group v-model="form.awardGrade">
            <el-radio
              v-for="dict in dict.type.pm_award_grade"
              :key="dict.value"
:label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="颁奖部门" prop="awardDepartment" style="width:320px;">
          <el-input v-model="form.awardDepartment" placeholder="请输入颁奖部门" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
        <el-form-item label="获奖日期" prop="awardDate">
          <el-date-picker clearable
            v-model="form.awardDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择获奖日期">
          </el-date-picker>
        </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="到期日期" prop="expireDate">
              <el-date-picker clearable
                              v-model="form.expireDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择到期日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>

          <el-col :span="12">
            <el-form-item label="负责人" prop="leader" style="width:320px;">
              <el-input v-model="form.leader" placeholder="请输入负责人" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="证书"  prop="image" >
          <image-upload v-model="form.image"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCurrent_project_award, getCurrent_project_award, delCurrent_project_award, addCurrent_project_award, updateCurrent_project_award } from "@/api/business/current_project_award";
import { listCurrent_project, getCurrent_project} from "@/api/business/current_project";

export default {
  name: "Current_project_award",
  dicts: ['pm_award_grade'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      //高级搜索
      showHighSearch: false,
      // 总条数
      total: 0,
      // 项目获奖表格数据
      current_project_awardList: [],
      options: [],
      projectOptions:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 备注时间范围
      daterangeAwardDate: [],
      daterangeExpireDate: [],
      // 排序
      sortField: "",
      sort: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectId: null,
        awardName: null,
        awardGrade: null,
        awardDepartment: null,
        awardDate: null,
        leader: null,
        projectName:null,
      },
      //project查询参数
      queryProjectParams: {
        pageNum: 1,
        pageSize: 10,
        projectName: null,
      },
      option: {
        projectId: null,
        projectName: null,
      },
      projectOption: {
        projectId: null,
        projectName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        projectId: [
          { required: true, message: "项目名称不能为空", trigger: "change" }
        ],
        awardName: [
          { required: true, message: "获奖名称不能为空", trigger: "blur" }
        ],
        awardGrade: [
          { required: true, message: "获奖等级不能为空", trigger: "blur" }
        ],
        awardDepartment: [
          { required: true, message: "颁奖部门不能为空", trigger: "blur" }
        ],
        awardDate: [
          { required: true, message: "获奖日期不能为空", trigger: "blur" }
        ],
        image: [
          { required: true, message: "证书不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    const tempProjectId = this.$route.params && this.$route.params.projectId;

    if(tempProjectId!=null){
      getCurrent_project(tempProjectId).then(response => {
        this.projectOptions.projectName=response.data.projectName;
        this.projectOptions.projectId=response.data.projectId;
        //this.options.push(this.options);
        this.queryParams.projectId = parseInt(tempProjectId);
        this.getList();
      });
    }else{
      this.getList();
    }

    listCurrent_project().then(response => {
      this.projectOptions = response.rows;
    });

  },
  methods: {
    //排序
    sortChange(column, prop, order) {
      // ascending 升序
      this.sort =
        column.order == "ascending"
          ? "asc" : "desc";
      this.sortField =column.prop;
      this.getList();
    },
    /** 查询项目获奖列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeAwardDate && '' != this.daterangeAwardDate) {
        this.queryParams.params["beginAwardDate"] = this.daterangeAwardDate[0];
        this.queryParams.params["endAwardDate"] = this.daterangeAwardDate[1];
      }
      if (null != this.daterangeExpireDate && '' != this.daterangeExpireDate) {
        this.queryParams.params["beginExpireDate"] = this.daterangeExpireDate[0];
        this.queryParams.params["endExpireDate"] = this.daterangeExpireDate[1];
      }
      if (null != this.sortField && '' != this.sortField && null != this.sort && '' != this.sort) {
        this.queryParams.params["sortField"] = this.sortField;
        this.queryParams.params["sort"] = this.sort;
      }
      listCurrent_project_award(this.queryParams).then(response => {
        this.current_project_awardList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getProjectList(query) {
      if (query !== '') {
        this.loading = true;
        this.queryProjectParams.projectName = query;
        listCurrent_project(this.queryProjectParams).then(response => {
          this.loading = false;
          this.options = response.rows;
        });
      } else {
        this.options = [];
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        awardId: null,
        projectId: null,
        awardName: null,
        awardGrade: "0",
        awardDepartment: null,
        awardDate: null,
        expireDate: null,
        leader: null,
        image: null,
        createTime: null,
        options: [],
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeAwardDate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.awardId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加项目获奖";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.option.projectName=row.projectName;
      this.option.projectId=row.projectId;
      this.options.push(this.option);
      const awardId = row.awardId || this.ids
      getCurrent_project_award(awardId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改项目获奖";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.awardId != null) {
            updateCurrent_project_award(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCurrent_project_award(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const awardIds = row.awardId || this.ids;
      var deleteTitle = row.awardName? ('"'+row.awardName+'"'):'';
      this.$modal.confirm('是否确认删除' + deleteTitle + '？').then(function() {
        return delCurrent_project_award(awardIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/current_project_award/export', {
        ...this.queryParams
      }, `current_project_award_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
